<?php
class classPhotoAlbum extends phpajax{
	private $sDir;
	
	function __construct(){
            $this->sDir = 'data/';
	}
	
	function input(){
            aread("cb_photoalbum_list"); 
	}
	
	function main() {
            $sShowDir 	= & $this->cb_photoalbum_list;
            aprint('photoalbum_container', '<br>'.'<br>'.self::getPhotosList($this->sDir,$sShowDir));
	}
	
        /**
         * function returns list of photoalbums stored in combobox
         *<br><br><b>return value</b> of this function <b>is combobox with ajax response on change</b>!
         */
	public function getPhotoAlbumsList(){
            $_fileSystem = new classFileSystem();
            return self::getComboBox($_fileSystem->getDirsList($this->sDir));
	}

        /**
         * function generates combobox html element with ajax response on change!
         */
	private function getComboBox($DirList){
            if($DirList=='' or count($DirList)==0) 
              return _i18n_imageviewer_NO_PHOTOALBUM_WAS_FOUND_;
            
            array_multisort($DirList,SORT_STRING);
            $result = '
              <select
                name="cb_photoalbum_list"
                id="cb_photoalbum_list"
                onchange="classPhotoAlbum();"
              >
            ';
            
            $_XMLService = new classImageViewerXMLService();

            foreach ($DirList as $value) {
              $_XMLService->getXMLDataDOM($this->sDir,$value,&$sItemName,&$sItemDescription);
              $result .= '
                <option value="'.$value.'">'.$sItemName.'</option>
              ';
            }		

            $result .= '
              </select>
            ';

            return $result;
	}
	
	private function getPhotosList($baseDir, $selectedDir){
            $dirname = $baseDir.$selectedDir.'/';
            if(!is_dir($dirname)) return '';

            //create photoalbums description
            $_XMLService = new classImageViewerXMLService();
            $_XMLService->getXMLDataDOM($baseDir,$selectedDir,&$sItemName,&$sPhotoalbumDescription);

            //create array of files            
            $_fileSystem = new classFileSystem();
            $filesList = $_fileSystem->getFilesList($dirname,array('jpg','jpeg','bmp','png','gif'));

            if(count($filesList)==0 or (count($filesList)==1 and $filesList==''))
              return _i18n_imageviewer_PHOTOALBUM_IS_EMPTY_;

            //sort array of files
            array_multisort($filesList, SORT_STRING);

            //generate images list
            $result = '
            <center>
            '.$sPhotoalbumDescription.'
            <br>
            <br>

            <br>
            <br>
            <table class="css_product_mini_main_container">
              <tr>
            ';

            $Files_Count=count($filesList);
            $iNo = 0;
            foreach ($filesList as $filename) {
              //create photoalbums description
              $sImageName = '';
              $_XMLService->getXMLDataDOM($dirname,$filename,&$sImageName,&$sImageDescription);

              $iNo++;
              $result .= '
                <td style="font-family:verdana;font-size:10px;vertical-align:bottom;text-align:center;">
                  <img
                    src="'.$dirname.$filename.'"
                    style="width:80px; length:60px; cursor: pointer;"
                    onclick="
                        document.getElementById(\'Show_Product_Image\').value=\''.$dirname.$filename.'\';
                        classImageViewer();
                    "
                  >
                  <br>
                  '.$sImageName.'
                </td>
            ';
            if(!($iNo % _PRODUCTS_COUNT_IN_SINGLE_LINE_) and ($iNo < $Files_Count))
              $result .= '
                </tr>
                <tr>
              ';
            }	

            $result .= '
              </table>
              <br>
              <br>

              </center>
            ';

            return $result;
	}
        
        public function getPageSelector($iItemsCount, $iItemPerPage, $iMaxPages, $iSelectedPage){
            $iTotalPagesCount = round(($iItemsCount/$iItemPerPage),0);
            if($iTotalPagesCount>1){
                $result = '1';
                for($iIndex=1;$iIndex<$iTotalPagesCount;$iIndex++){
                    $result .= ' - '.(string)($iIndex+1);                    
                }
            }
            return $result;
        }
}
?>